package com.whsr.readservice.dao;

import com.whsr.readservice.dto.CategoryRequest;
import com.whsr.readservice.vo.Category;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface CategoryMapper {



    @Select("select " +
            " id as id, " +
            " name as name, " +
            " p_id as pId, " +
            " code as code, " +
            " data_flag as dataFlag, " +
            " create_time as createTime, " +
            " update_time as updateTime " +
            "from category a where a.data_flag = #{dataFlag}")
    List<Category> selectCategoryByDataFlag(@Param("dataFlag") Integer dataFlag);

    @Select("select " +
            " id as id, " +
            " name as name, " +
            " p_id as pId, " +
            " code as code, " +
            " data_flag as dataFlag, " +
            " create_time as createTime, " +
            " update_time as updateTime " +
            "from category a where a.p_id  = #{pId}")
    List<Category> selectCategoryByPid(@Param("pId") Integer pId);

    @Select("<script>" +
            "select" +
            " t.id, " +
            " b.`name` AS parentCategoryName, " +
            " t.name," +
            " t.`data_flag` AS dataFlag " +
            "FROM category t, category b " +
            " where t.`p_id` = b.`id` " +
            "<when test='id!=null'>" +
            " and t.id = #{id} " +
            "</when>" +
            "<when test='dataFlag!=null'>" +
            " and t.data_flag = #{dataFlag} " +
            "</when>" +
            "<when test='pId!=null'>" +
            " and t.p_id = #{pId} " +
            "</when>" +
            "</script>")
    List<CategoryRequest> selectCategoryList(CategoryRequest categoryRequest);

    @Select("select name , id, p_id as pid from category")
    List<Map<String, Object>> getAllCategory();
}
